home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_09_12
/
9n12022a
< prev
next >
Wrap
Text File
|
1991-10-13
|
3KB
|
105 lines
void idle(unsigned priority)
{
printf("idle @ %3u\n", priority);
}
/* ----------------------------------------------------------- */
void pro1(unsigned priority)
{
printf("pro1 @ %3u\n", priority);
}
/* ----------------------------------------------------------- */
void pro2(unsigned priority)
{
printf("pro2 @ %3u\n", priority);
}
/* ----------------------------------------------------------- */
void pro3(unsigned priority)
{
printf("pro3 @ %3u\n", priority);
}
/* --------------------------------------------------------------
FUNCTION PROC_CMD: The steps to process a command are:
Note that handlers registered using signal require one int argument.
When a handler is invoked, the argument passed to it is the type of
signal that caused its invocation. Since, in this case it is always
SIGINT, the argument is never used within this handler.
actions is a jump table whose index is the menu option chosen by
the user.
A. Since we have just trapped a SIGINT signal, the library will now
revert to using the SIG_DFL method for future SIGINT signals that
occur while this signal handler is executing. This default
method generally results in the program being abnormally
terminated and that is unsuitable behavior here. As such, we ask
to have such signals ignored until we are again ready to them.
B. Prompt the user for their menu choice.
C. Call the corresponding function.
D. Reregister this handler for future SIGINT signals.
-------------------------------------------------------------- */
void proc_cmd(int dummy)
{
int code;
static const void (*actions[])(void) = {
myexit,
help,
add_node
};
/*A*/ if (signal(SIGINT, SIG_IGN) == SIG_ERR) {
fprintf(stderr, "can't ignore signal\n");
exit(EXIT_FAILURE);
}
while (1) {
/*B*/ printf("\nEnter Action Code (1 for help): ");
scanf("%d", &code);
if (code < 0 || code >= NUMELEM(actions)) {
fprintf(stderr, "\n Invalid command\n");
continue;
}
/*C*/ (*actions[code])(); /* call selected action */
break;
}
/*D*/ if (signal(SIGINT, proc_cmd) == SIG_ERR) {
fprintf(stderr, "can't register handler\n");
exit(EXIT_FAILURE);
}
}
/* ----------------------------------------------------------- */
void myexit(void)
{
exit(EXIT_SUCCESS);
}
/* ----------------------------------------------------------- */
void help(void)
{
printf("\n The action codes are:\n");
printf("\t0 - Exit this program\n");
printf("\t1 - Produce this help message\n");
printf("\t2 - Add a node\n");
}